package com.mohe.dangjian.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.model.Remind;
import com.mohe.dangjian.service.interfaces.IRemind;
import com.mohe.dangjian.utils.Json;
import com.mohe.dangjian.utils.JsonUtil;
import com.mohe.dangjian.utils.Page;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 提醒Action
 * 
 * @author wxm
 * 
 */
public class RemindAction extends ActionSupport {

	private static final long serialVersionUID = -6675908377860934509L;

	private IRemind remindServ;
	
	private List<Remind> remindList;
	private List<Remind> thisMonthRemindList; // 当月的提醒列表
	private Page page = new Page();
	private Remind remind = new Remind();
	
	private String flag;
	// 表示从哪跳转到编辑页面，1:主页面/2:查看页面
	private Integer editFrom;

	public IRemind getRemindServ() {
		return remindServ;
	}

	public void setRemindServ(IRemind remindServ) {
		this.remindServ = remindServ;
	}

	public List<Remind> getRemindList() {
		return remindList;
	}

	public void setRemindList(List<Remind> remindList) {
		this.remindList = remindList;
	}

	public List<Remind> getThisMonthRemindList() {
		return thisMonthRemindList;
	}

	public void setThisMonthRemindList(List<Remind> thisMonthRemindList) {
		this.thisMonthRemindList = thisMonthRemindList;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public Remind getRemind() {
		return remind;
	}

	public void setRemind(Remind remind) {
		this.remind = remind;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public Integer getEditFrom() {
		return editFrom;
	}

	public void setEditFrom(Integer editFrom) {
		this.editFrom = editFrom;
	}

	/**
	 * 获取提醒信息列表
	 * @return
	 */
	public String getRemindListForPage() {
		int count = remindServ.getCount(remind);
		page.setCount(count);
		remindList = remindServ.getRemindList(remind, page);
		return "remind.jsp";
	}
	
	/**
	 * 获取提醒信息列表
	 * @return
	 */
	public String getRemindListToShow() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		
		remind.setRemindDate(df.format(date));
		int count = remindServ.getCount(remind);
		page.setPageSize(3);
		page.setCount(count);
		remindList = remindServ.getRemindList(remind, page);
		
		String thisMonth = df.format(date);
		thisMonth = thisMonth.substring(0, thisMonth.lastIndexOf("-"));
		Remind newRemind = new Remind();
		remind.setHappenDate(thisMonth);
		thisMonthRemindList = remindServ.getRemindList(newRemind);
		
		return "main.jsp";
	}
	
	/**
	 * 分页
	 */
	public void searchRemindList() {
		Json json = new Json();
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		int count = remindServ.getCount(remind);
		page.setCount(count);
		List<Remind> list = remindServ.getRemindList(remind, page);
		if (list != null) {
			int index = (page.getCurrPage() - 1) * page.getPageSize() + 1;
			for (Remind s : list) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("index", index + "");
				if (s.getTimeKey() != null) {
					map.put("timeKey", s.getTimeKey() + "");
				} else {
					map.put("timeKey", "0");
				}
				if (!StringUtils.isBlank(s.getName())) {
					map.put("name", s.getName());
				} else {
					map.put("name", "");
				}
				if (!StringUtils.isBlank(s.getContent())) {
					map.put("content", s.getContent());
				} else {
					map.put("content", "");
				}
				if (!StringUtils.isBlank(s.getHappenDate())) {
					map.put("happenDate", s.getHappenDate());
				} else {
					map.put("happenDate", "");
				}
				if (!StringUtils.isBlank(s.getRemindDate())) {
					map.put("remindDate", s.getRemindDate());
				} else {
					map.put("remindDate", "");
				}
				mapList.add(map);
				index++;
			}
		}
		json.setObject(page);
		json.setMap(mapList);
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 跳转到编辑界面
	 * @return
	 */
	public String toEditRemind() {
		remind = remindServ.getRemindById(remind.getTimeKey());
		return "editRemind.jsp";
	}
	
	/**
	 * 更新项目信息
	 */
	public void updateRemind(){
		Json json = new Json();
		Remind newRemind = remindServ.getRemindById(remind.getTimeKey());
		newRemind.setName(remind.getName());
		newRemind.setContent(remind.getContent());
		newRemind.setHappenDate(remind.getHappenDate());
		newRemind.setRemindDate(remind.getRemindDate());
		newRemind.setCreateDate(new Date());
		boolean result = remindServ.updateRemind(newRemind);
		if (result) {
			json.setMessage("编辑成功");
		} else {
			json.setSuccess(true);
			json.setMessage("编辑失败");
		}
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 保存新增提醒
	 */
	public void saveRemind() {
		Json json = new Json();
		boolean result = remindServ.updateRemind(remind);
		if (result) {
			json.setMessage("新增成功");
		} else {
			json.setSuccess(true);
			json.setMessage("新增失败");
		}
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 删除提醒
	 */
	public void delRemind() {
		Json json = new Json();
		boolean result = remindServ.delRemind(remind.getTimeKey());
		if (result) {
			json.setMessage("删除成功");
		} else {
			json.setSuccess(true);
			json.setMessage("删除失败");
		}
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 跳转到详细界面
	 * @return
	 */
	public String toShowRemind() {
		remind = remindServ.getRemindById(remind.getTimeKey());
		return "showRemind.jsp";
	}
}
